package com.hyzx.qbasic.admin.controller.cms;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.constant.ProcessCategoryKeyConst;
import com.hyzx.qbasic.admin.model.constant.ProcessConfigKeyConst;
import com.hyzx.qbasic.admin.model.po.CmsChannelInfoPO;
import com.hyzx.qbasic.admin.model.po.CmsChannelPO;
import com.hyzx.qbasic.admin.model.po.ProcessConfigPO;
import com.hyzx.qbasic.admin.model.po.ProcessDefinitionPagedInfoPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.ICmsChannelService;
import com.hyzx.qbasic.admin.domain.service.IProcessConfigService;
import com.hyzx.qbasic.admin.domain.service.IProcessDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2018/7/25
 */


@Api(tags = "栏目管理", description = "ChannelController")
@Controller
@RequestMapping("/cms/channel")
public class ChannelController extends BaseController {

    @Autowired
    private ICmsChannelService channelService;

    @Autowired
    private IProcessDefinitionService processDefinitionService;

    @Autowired
    private IProcessConfigService processConfigService;

    @ApiIgnore
    @SysLog("栏目管理首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.CMS_CHANNEL_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.CMS_CHANNEL_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.CMS_CHANNEL_DELETE));
        return "cms/channel/list";
    }

    @ApiOperation("获取栏目列表")
    @GetMapping("/getChannelList")
    @ResponseBody
    public BaseResModel<PageInfo<CmsChannelInfoPO>> getChannelList(@Valid GetCmsChannelListReq req, BindingResult result) {
        return BaseResUtils.success(channelService.getPagedCmsChannelList(req));
    }

    @ApiIgnore
    @SysLog("新增栏目页")
    @GetMapping("/add")
    public String add() {
        return "cms/channel/add";
    }

    @ApiOperation("新增栏目表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddCmsChannelReq req, BindingResult result) {
        BaseOperationRes res = channelService.addCmsChannel(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取全部栏目列表")
    @GetMapping("/getAllChannelList")
    @ResponseBody
    public BaseResModel<List<CmsChannelPO>> getAllChannelList() {
        return BaseResUtils.success(channelService.getAllCmsChannelList());
    }

    @ApiOperation("删除栏目")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteCmsChannelReq req, BindingResult result) {
        BaseOperationRes res = channelService.deleteCmsChannel(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑栏目页")
    @GetMapping("/edit")
    public String edit() {
        return "cms/channel/edit";
    }

    @ApiOperation("编辑栏目表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditCmsChannelReq req, BindingResult result) {
        BaseOperationRes res = channelService.editCmsChannel(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取栏目信息")
    @GetMapping("/getChannelInfo")
    @ResponseBody
    public BaseResModel<CmsChannelPO> getChannelInfo(@Valid GetCmsChannelInfoReq req, BindingResult result) {
        return BaseResUtils.success(channelService.getCmsChannelInfo(req.getChannelId()));
    }

    @ApiOperation("获取内容发布流程定义列表")
    @GetMapping("/getCmsProcessList")
    @ResponseBody
    public BaseResModel<List<ProcessDefinitionPagedInfoPO>> getCmsProcessList() {
        return BaseResUtils.success(
                processDefinitionService.getProcessDefinitionListByCategoryKey(
                        ProcessCategoryKeyConst.PROCESS_CATEGORY_KEY_CMS
                ));
    }

    @ApiOperation("获取内容发布流程配置信息")
    @GetMapping("/getCmsProcessConfigInfo")
    @ResponseBody
    public BaseResModel<ProcessConfigPO> getCmsProcessConfigInfo(@Valid GetCmsChannelInfoReq req, BindingResult result) {
        return BaseResUtils.success(
                processConfigService.getProcessConfigByKey(
                        ProcessConfigKeyConst.PROCESS_CONFIG_KEY_CMS + req.getChannelId()
                ));
    }
}
